package duppentity.dto.file;

import com.alibaba.fastjson.JSONObject;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import utils.JSONObjectUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author yangzhi
 * @date 2019/7/5
 */
public class FileUtil {

	public static List<ImageGroupDto> getImageGroupListDto(JsonObject data){
		List<ImageGroupDto> ImageGroupDtoList = null;
		if (data!=null) {
			if (data.has("total")) {
				if (data.get("total").getAsInt() > 0) {
					JsonArray list = data.getAsJsonArray("list");
					if (list != null && list.size() > 0) {
						ImageGroupDtoList = new ArrayList<>();
						for (Integer i=0;i<list.size();i++) {
							JSONObject listOne = JSONObjectUtils.get(list, i);
							ImageGroupDto adDto = convertImageGroupDto(listOne);
							ImageGroupDtoList.add(adDto);
						}
					}
				}
			}
		}
		return  ImageGroupDtoList;
	}

	public static ImageGroupDto getImageGroupDto(JsonObject data) {
		ImageGroupDto ImageGroupDto =new ImageGroupDto();
		if (data!=null) {
			if (data.has("total")) {
				if (data.get("total").getAsInt() > 0) {
					JsonArray list = data.getAsJsonArray("list");
					JSONObject listOne = JSONObjectUtils.get(list, 0);
					ImageGroupDto = convertImageGroupDto(listOne);
				}
			}
		}
		return ImageGroupDto;
	}

	/**
	 *
	 * @param listOne
	 * @param
	 */
	private static ImageGroupDto convertImageGroupDto(JSONObject listOne){
		ImageGroupDto imageGroupDto = new ImageGroupDto();
		imageGroupDto.id = listOne.getString("id");
		imageGroupDto.sysFlag = listOne.getIntValue("sysFlag");
		imageGroupDto.name = listOne.getString("name");
		imageGroupDto.createTime = listOne.getLong("createTime");
		imageGroupDto.modifyTime = listOne.getLong("modifyTime");
		return imageGroupDto;
	}



	public static List<VideoGroupDto> getVideoGroupListDto(JsonObject data){
		List<VideoGroupDto> videoGroupDtoList = null;
		if (data!=null) {
			if (data.has("total")) {
				if (data.get("total").getAsInt() > 0) {
					JsonArray list = data.getAsJsonArray("list");
					if (list != null && list.size() > 0) {
						videoGroupDtoList = new ArrayList<>();
						for (Integer i=0;i<list.size();i++) {
							JSONObject listOne = JSONObjectUtils.get(list, i);
							VideoGroupDto videoGroupDto = convertVideoGroupDto(listOne);
							videoGroupDtoList.add(videoGroupDto);
						}
					}
				}
			}
		}
		return  videoGroupDtoList;
	}

	public static VideoGroupDto getVideoGroupDto(JsonObject data) {
		VideoGroupDto videoGroupDto =new VideoGroupDto();
		if (data!=null) {
			if (data.has("total")) {
				if (data.get("total").getAsInt() > 0) {
					JsonArray list = data.getAsJsonArray("list");
					JSONObject listOne = JSONObjectUtils.get(list, 0);
					videoGroupDto = convertVideoGroupDto(listOne);
				}
			}
		}
		return videoGroupDto;
	}

	/**
	 *
	 * @param listOne
	 * @param
	 */
	private static VideoGroupDto convertVideoGroupDto(JSONObject listOne){
		VideoGroupDto videoGroupDto = new VideoGroupDto();
		videoGroupDto.id = listOne.getString("id");
		videoGroupDto.sysFlag = listOne.getIntValue("sysFlag");
		videoGroupDto.name = listOne.getString("name");
		videoGroupDto.createTime = listOne.getLong("createTime");
		videoGroupDto.modifyTime = listOne.getLong("modifyTime");
		return videoGroupDto;
	}




	public static List<VideoDto> getVideoListDto(JsonObject data){
		List<VideoDto> videoDtoList = null;
		if (data!=null) {
			if (data.has("total")) {
				if (data.get("total").getAsInt() > 0) {
					JsonArray list = data.getAsJsonArray("list");
					if (list != null && list.size() > 0) {
						videoDtoList = new ArrayList<>();
						for (Integer i=0;i<list.size();i++) {
							JSONObject listOne = JSONObjectUtils.get(list, i);
							VideoDto videoDto = convertVideoDto(listOne,data);
							videoDtoList.add(videoDto);
						}
					}
				}
			}
		}
		return  videoDtoList;
	}


	public static VideoDto getVideoDto(JsonObject data) {
		VideoDto videoDto =new VideoDto();
		if (data!=null) {
			if (data.has("total")) {
				if (data.get("total").getAsInt() > 0) {
					JsonArray list = data.getAsJsonArray("list");
					JSONObject listOne = JSONObjectUtils.get(list, 0);
					videoDto = convertVideoDto(listOne,data);
				}
			}
		}
		return videoDto;
	}


	private static VideoDto convertVideoDto(JSONObject listOne,JsonObject data){
		VideoDto videoDto = new VideoDto();
		videoDto.id = listOne.getString("id");
		videoDto.shopId = listOne.getString("shopId");
		videoDto.groupId = listOne.getString("groupId");
		videoDto.name = listOne.getString("name");
		videoDto.oldName = listOne.getString("oldName");
		videoDto.extName = listOne.getString("extName");
		videoDto.playTime = listOne.getIntValue("playtime");
		videoDto.imageId = listOne.getLong("imageId");
		videoDto.size = listOne.getIntValue("size");
		videoDto.url = listOne.getString("url");
		if (videoDto.imageId>0){
			JSONObject imageDto = JSONObjectUtils.get(data, "sh_image");
			videoDto.imageUrl = JSONObjectUtils.getString(imageDto,listOne.getString("imageId"),"url");
			videoDto.imageName = JSONObjectUtils.getString(imageDto,listOne.getString("imageId"),"name");
			videoDto.imageExtName = JSONObjectUtils.getString(imageDto,listOne.getString("imageId"),"extName");

		}
		videoDto.createTime = listOne.getLong("createTime");
		videoDto.modifyTime = listOne.getLong("modifyTime");
		return videoDto;
	}



	public static List<ImageDto> getImageListDto(JsonObject data){
		List<ImageDto> imageDtoList = null;
		if (data!=null) {
			if (data.has("total")) {
				if (data.get("total").getAsInt() > 0) {
					JsonArray list = data.getAsJsonArray("list");
					if (list != null && list.size() > 0) {
						imageDtoList = new ArrayList<>();
						for (Integer i=0;i<list.size();i++) {
							JSONObject listOne = JSONObjectUtils.get(list, i);
							ImageDto imageDto = convertImageDto(listOne);
							imageDtoList.add(imageDto);
						}
					}
				}
			}
		}
		return  imageDtoList;
	}

	private static ImageDto convertImageDto(JSONObject listOne){
		ImageDto imageDto = new ImageDto();
		imageDto.id = listOne.getString("id");
		imageDto.shopId = listOne.getString("shopId");
		imageDto.groupId = listOne.getString("groupId");
		imageDto.name = listOne.getString("name");
		imageDto.oldName = listOne.getString("oldName");
		imageDto.extName = listOne.getString("extName");
		imageDto.height = listOne.getIntValue("height");
		imageDto.width = listOne.getIntValue("width");
		imageDto.size = listOne.getIntValue("size");
		imageDto.url = listOne.getString("url");
		imageDto.createTime = listOne.getLong("createTime");
		imageDto.modifyTime = listOne.getLong("modifyTime");
		return imageDto;
	}
}
